package com.jinxinol.core.util.encryption;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import com.jinxinol.core.util.StringUtils;

/**
 * <p>对称DES加密工具类。</p>
 * 
 */
public class DESUtils {
	/**
	 * DES算法名称
	 */
	private static String ALGORITHM_NAME = "DES";
	
	/**
	 * 缺省字符编码
	 */
	private static String DEFAULT_ENCODING = "UTF-8";
	
	/**
	 * 密钥
	 */
	private static SecretKey SECRET_KEY = null;

	static{
		SECRET_KEY = readKey();
	}

	/**
	 * <p>生成密钥文件，保存至指定的文件<p/>
	 * @param desKeyFile 密钥文件
	 */
	public static void generateKey(String desKeyFile){
		ObjectOutputStream out = null;
		try {
			KeyGenerator keygen = KeyGenerator.getInstance(ALGORITHM_NAME);
			SecretKey secretKey = keygen.generateKey();
			
			out = new ObjectOutputStream(new FileOutputStream(desKeyFile));
			out.writeObject(secretKey); 
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(out != null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 读取密钥文件
	 * @return 密钥对象
	 */
	public static SecretKey readKey(){
		ObjectInputStream in = null;
		try {
			in = new ObjectInputStream(DESUtils.class.getResourceAsStream("deskey.dat"));
			SecretKey secretKey =(SecretKey)in.readObject(); 
			return secretKey;
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(in != null){
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 按DES算法进行加密
	 * @param info 明文
	 * @param encoding 明文的字符编码
	 * @return 密文
	 */
	public static String encrypt(String info, String encoding) {
		try {
			Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
			cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY);
			byte[] tytes = cipher.doFinal(info.getBytes(encoding));
			return StringUtils.bytesHEX(tytes);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	/**
	 * 按DES算法进行加密
	 * @param info 明文（明文的缺省编码是UTF-8）
	 * @return 密文
	 */
	public static String encrypt(String info){
		return encrypt(info, DEFAULT_ENCODING);
	}
	
	/**
	 * 按DES算法进行解密
	 * @param ciphertext 密文
	 * @param encoding 明文的字符编码
	 * @return 明文
	 */
	public static String decrypt(String ciphertext, String encoding) {
		try {
			Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
			cipher.init(Cipher.DECRYPT_MODE, SECRET_KEY);
			byte[] bytes = cipher.doFinal(StringUtils.hexBytes(ciphertext));
			return new String(bytes, encoding);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 按DES算法进行解密
	 * @param ciphertext 密文
	 * @return 明文（明文的缺省编码是UTF-8）
	 */
	public static String decrypt(String ciphertext){
		return decrypt(ciphertext, DEFAULT_ENCODING);
	}

	public static void main(String[] args) {
		String[] strs = new String[4];
		strs[0] = "aa1";
		strs[1] = "aabbcaabbc";
		strs[2] = "我";
		strs[3] = "我是中国人我是中国人";
		
		for(String str : strs){
			String info = str;
			
			System.out.println("加密前的明文：" + info);
			System.out.println("加密前的明文 length：" + info.length());
			
			String ciphertext = DESUtils.encrypt(info);
			System.out.println("加密后的密文：" + ciphertext);
			System.out.println("加密后的密文 length：" + ciphertext.length());
			System.out.println("=======================================");
		}
	}
}
